package ass.dao.support;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import ass.beans.UserBean;
import ass.beans.UserFriendsBean;
import ass.beans.UserPrivacyBean;
import ass.common.DBConnectionFactory;
import ass.common.ServiceLocatorException;
import ass.dao.DataAccessException;
import ass.dao.UserFriendsDAO;

public class UserFriendsDAOImpl implements UserFriendsDAO {
	
	/**
	 * The service locator to retrieve database connections from
	 */
	private DBConnectionFactory services;
	
	/** Creates a new instance of UserDAOImpl */
	public UserFriendsDAOImpl() {
		try {
			services = new DBConnectionFactory();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
		}
	}

	public UserFriendsDAOImpl(DBConnectionFactory services) {
		this.services = services;
	}
	
	public void insert(UserFriendsBean bean) throws DataAccessException {
		Connection con = null;
		   try { 
		 con = services.createConnection();
	     PreparedStatement stmt = con.prepareStatement(
	       "INSERT INTO PUBLIC.TBL_USER_FRIENDS" +
			"(USERNAME, USERNAME_FRIEND, VERIFICATION_CODE, IS_ACCEPTED, IS_REJECTED, MESSAGE)"+
		    "VALUES (?, ?, ?, ?, ?, ?)");
	     stmt.setString(1, bean.getUsername());
	     stmt.setString(2, bean.getUsernameFriend());
	     stmt.setString(3, bean.getVerificationCode());
	     stmt.setBoolean(4, bean.isAccepted());
	     stmt.setBoolean(5, bean.isRejected());
	     stmt.setString(6, bean.getMessage());
	     int n = stmt.executeUpdate();

	     stmt.close();
	     if (n != 1)
	       throw new DataAccessException("Did not insert one row into database");
	   } catch (ServiceLocatorException e) {
	       throw new DataAccessException("Unable to retrieve connection; " + e.getMessage(), e);
	   } catch (SQLException e) {
	       throw new DataAccessException("Unable to execute query; " + e.getMessage(), e);
	   } finally {
	      if (con != null) {
	         try {
	           con.close();
	         } catch (SQLException e1) {
	           e1.printStackTrace();
	         }
	      }
	   }
	}
	
	public void update (String verificationCode, UserFriendsBean bean) throws DataAccessException {
		
		Connection con = null;
		   try {
			    con = services.createConnection();
				String sql = "UPDATE TBL_USERS "+
							 "SET "+
							"	IS_ACCEPTED = "+bean.isAccepted() +", "+
							"	IS_REJECTED = "+bean.isRejected() +
							" WHERE verification_code = ?";
				System.out.println(sql);
				
	            PreparedStatement stmt = con.prepareStatement(sql.toString());
	            // run the SQL statement
	            stmt.setString(1, verificationCode);
	            int result = stmt.executeUpdate();
	            if (result == 0) {
	                System.out.println("Customer not updated.");
	            } else {
	                System.out.println("Customer updated.");
	            }
	            stmt.close();
		   } catch (ServiceLocatorException e) {
				throw new DataAccessException("Unable to retrieve connection; " + e.getMessage(), e);
		   } catch (SQLException e) {
				throw new DataAccessException("Unable to execute query; " + e.getMessage(), e);
		   } finally {
				if (con != null) {
					try {
						con.close();
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				}
			}
		
	}

	public List<UserFriendsBean> getUserFriendsAccepted(String username)
			throws DataAccessException {
		List<UserFriendsBean> list = new ArrayList<UserFriendsBean>();
		Connection con = null;
		try {
			con = services.createConnection();
			// convert('2003-07-08',DATE)
			PreparedStatement stmt = con
					.prepareStatement("select * from TBL_USER_FRIENDS where username = ? and is_accepted = true");
			stmt.setString(1, username);
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				UserFriendsBean userFriendsBean = createUserFriendsBean(rs);
				list.add(userFriendsBean);
			}
			stmt.close();
		} catch (ServiceLocatorException e) {
			throw new DataAccessException("Unable to retrieve connection; "
					+ e.getMessage(), e);
		} catch (SQLException e) {
			throw new DataAccessException("Unable to execute query; "
					+ e.getMessage(), e);
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		}

		return list;
	}
	
	public UserFriendsBean getUserFriendRow(String username, String usernameFriend) throws DataAccessException {

		   Connection con = null;
		   try {
		     con = services.createConnection();
		     PreparedStatement stmt = con.prepareStatement(
		       "select * from TBL_USER_FRIENDS where USERNAME = ? and USERNAME_FRIEND = ?");
		     stmt.setString(1, username);
		     stmt.setString(2, usernameFriend);
		     ResultSet rs = stmt.executeQuery();
				if (rs.next()) {
					System.out.println("getUSer");
					UserFriendsBean user = createUserFriendsBean(rs);
					return user;
				}
				stmt.close(); 
			} catch (ServiceLocatorException e) {
				throw new DataAccessException("Unable to retrieve connection; " + e.getMessage(), e);
			} catch (SQLException e) {
				throw new DataAccessException("Unable to execute query; " + e.getMessage(), e);
			} finally {
				if (con != null) {
					try {
						con.close();
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				}
			}
			return null;
		}
	
	
	
	private UserFriendsBean createUserFriendsBean(ResultSet rs) throws SQLException {
		UserFriendsBean userFriendsBean = new UserFriendsBean();
		userFriendsBean.setUsername(rs.getString("USERNAME") );
		userFriendsBean.setUsernameFriend(rs.getString("USERNAME_FRIEND"));
		userFriendsBean.setVerificationCode(rs.getString("VERIFICATION_CODE") );
		userFriendsBean.setAccepted(rs.getBoolean("IS_ACCEPTED"));
		userFriendsBean.setRejected(rs.getBoolean("IS_REJECTED"));
		userFriendsBean.setMessage(rs.getString("MESSAGE"));
		return userFriendsBean;
	}

}
